<template>
	<view :data-theme="theme">
		<view class="new-users copy-data">
			<view class="mid" style="flex:1;overflow: hidden;">
				<view class="bg">个人中心</view>
				<view class="head pad30">
					<view class="user-info">
						<image class="avatar" :src='userInfo.avatar' v-if="userInfo.avatar && uid" @click="goEdit()">
						</image>
						<image v-else class="avatar"
							src="https://lives-1308318918.cos.ap-shanghai.myqcloud.com/images/f.png" mode=""
							@click="goEdit()"></image>
						<view class="info">
							<view class="name" v-if="!uid" @tap="openAuto">
								请点击登录
							</view>
							<view class="name" v-if="uid">
								{{userInfo.nickname}}
								<view class="vip" v-if="userInfo.vip">
									<image :src="userInfo.vipIcon" alt="">
										<view style="margin-left: 10rpx;" class="vip-txt">{{userInfo.vipName || ''}}
										</view>
								</view>
							</view>
							<view class="num" v-if="userInfo.phone && uid">
								<view class="num-txt">
									<image
										src="https://lives-1308318918.cos.ap-shanghai.myqcloud.com/images/position.png" />
									<text>{{userInfo.provinceStr}}</text>
								</view>
								<view class="icon">
									<image src="https://lives-1308318918.cos.ap-shanghai.myqcloud.com/images/edit.png"
										mode=""></image>
								</view>
							</view>
							<view class="phone" v-if="!userInfo.phone && isLogin" @tap="bindPhone">绑定手机号</view>
						</view>
					</view>
					<view class="study-info-btn">
						<navigator url="/pages/students/contact/index">
							<button class="student-info" size="mini">
								<image
									src="https://lives-1308318918.cos.ap-shanghai.myqcloud.com/images/studentinfo.png">
								</image>
								<text v-if="userInfo.roleId == 0">学生信息</text>
								<text v-else>学生查询</text>
								
							</button>
						</navigator>
						<navigator url="/pages/summer/my_class/index">
							<button class="class-info" size="mini">
								<image src="https://lives-1308318918.cos.ap-shanghai.myqcloud.com/images/class.png">
								</image>
								<text>我的班级</text>
							</button>
						</navigator>
					</view>

					<!-- 家长角色的菜单信息 -->
					<view class="order-wrapper" v-if="userInfo.roleId == 0">
						<view class="order-bd">
							<block v-for="(item,index) in orderMenu" :key="index">
								<view class="order-item" @click="menusTap(item.url)">
									<view class="pic">
										<image class="iconfont pic_status" :src="item.img"></image>
										<text class="order-status-num" v-if="item.num > 0">{{item.num}}</text>
									</view>
									<view class="txt">{{item.title}}</view>
								</view>
							</block>
						</view>
					</view>

					<view class="function-list" v-if="userInfo.roleId == 0">
						<view class="list-item" v-for="(item, key) in mylist" @click="go(item)">
							<view class="item-left">
								<image :src="item.icon" class="img"></image>
								<view class="name">{{item.name}}</view>
							</view>
							<image src="https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/arrow.png"
								class="arrow_img"></image>
							<button
								style="background:#ffffff;text-align:left;position: absolute;width: 100%;font-size:32rpx;opacity: 0;"
								v-if="item.name=='联系我们'" open-type="contact">联系客服</button>
						</view>
					</view>
					
					<!-- 其他角色的信息展示 -->
					<view class="function-list" v-else>
						<view class="list-item border_line" v-for="(item, key) in info" :key="key">
							<view class="left_info">{{item.label}}</view>
							<view class="right_info">{{item.value}}</view>
						</view>
					</view>
					<!-- end -->
					
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	let sysHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
	import Cache from '@/utils/cache';
	import {
		goPage
	} from '@/libs/iframe.js'
	import {
		BACK_URL
	} from '@/config/cache';
	import {
		getMenuList,
		getUserInfo
	} from '@/api/user.js';
	import {
		orderData
	} from '@/api/order.js';
	import {
		getCity
	} from '@/api/api.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef H5
	import Auth from '@/libs/wechat';
	// #endif
	import {
		getShare
	} from '@/api/public.js';
	import {
		setThemeColor
	} from '@/utils/setTheme.js'
	import animationType from '@/utils/animationType.js'
	const app = getApp();
	export default {
		computed: mapGetters(['isLogin', 'chatUrl', 'uid']),
		data() {
			return {
				userInfo:{},
				orderMenu: [{
						img: 'https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/pay_0.png',
						title: '待付款',
						url: '/pages/summer/order_list/dtList?status=0',
						num: 0
					},
					{
						img: 'https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/pay_1.png',
						title: '待付尾款',
						url: '/pages/summer/order_list/dtList?status=1',
						num: 0
					},
					{
						img: 'https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/pay_1.png',
						title: '待开营',
						url: '/pages/summer/order_list/dtList?status=2',
						num: 0
					},
					{
						img: 'https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/pay_2.png',
						title: '售后',
						url: '/pages/summer/after_sale_order/dlist',
						num: 0
					},
					{
						img: 'https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/pay_3.png',
						title: '全部订单',
						url: '/pages/summer/order_list/dtList?status=3',
						num: 0
					}
				],
				mylist: [{
						icon: 'https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/pay_0.png',
						name: '购物车',
						url: '/pages/goods/order_addcart/order_addcart'
					},
					{
						name: '我的积分',
						icon: "https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/item_0.png",
						url: '/pages/sign/user_integral/index'
					},
					{
						name: '优惠卷',
						icon: "https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/item_1.png",
						url: '/pages/sign/user_coupon/index'
					},
					{
						name: '我的收藏',
						icon: "https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/item_2.png",
						url: '/pages/summer/collection/dlist'
					},
					{
						name: '联系我们',
						icon: "https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/item_3.png",
						url: '/pages/summer/contact/index'
					},
					{
						name: '商户入驻',
						icon: "https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/item_4.png",
						url: '/pages/summer/businesses/index'
					},
					{
						name: '我的分销',
						icon: "https://lives-1308318918.cos.ap-shanghai.myqcloud.com/img/center/item_5.png",
						url: '/pages/users/user_spread_user/index'
					},
				],
				imgUrls: [],
				userMenu: [],
				autoplay: true,
				circular: true,
				interval: 3000,
				duration: 500,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				orderStatusNum: {},
				MyMenus: [],
				wechatUrl: [],
				servicePic: 'https://lives-1308318918.cos.ap-shanghai.myqcloud.com/images/customer.png',
				sysHeight: sysHeight,
				pageHeight: '100%',
				configApi: {}, //分享类容配置
				theme: app.globalData.theme,
				bgColor: '#e93323',
				chatConfig: {
					consumer_hotline: '',
					telephone_service_switch: '0'
				}, //客服配置
				info: []
			}
		},
		onLoad() {
			if (app.globalData.isIframe) {
				setTimeout(() => {
					let active;
					document.getElementById('pageIndex').children.forEach(dom => {
						dom.addEventListener('click', (e) => {
							e.stopPropagation();
							e.preventDefault();
							if (dom === active) return;
							dom.classList.add('borderShow');
							active && active.classList.remove('borderShow');
							active = dom;
						})
					})
				});
			}
			let that = this;
			if (!this.$Cache.getItem('cityList')) this.getCityList();
			that.$set(that, 'MyMenus', app.globalData.MyMenus);
			that.$set(that, 'chatConfig', Cache.getItem('chatConfig'));
			that.bgColor = setThemeColor();
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: that.bgColor,
			});
		},
		onShow: function() {
			let that = this;
			uni.showTabBar();
			if (that.isLogin) {
				this.getMyMenus();
				this.getOrderData();
				this.getUser();
			} else {
				this.openAuto();
			}
		},
		methods: {
			/**
			 * 获取个人用户信息
			 */
			getUser: function() {
				let that = this;
				getUserInfo().then(res => {
					let userInfo = res.data;
					that.userInfo = userInfo;
					that.$store.commit("UPDATE_USERINFO", res.data);
					var reg = /.+?(省|市|自治区|自治州|县|区)/g;
					let province = this.userInfo.country.match(reg);
					if(province){
						this.userInfo.provinceStr = province.join("/")
					}else {
						this.userInfo.provinceStr = "";
					}
					//判断角色信息
					let info = ["性别", "手机号", "职位", "营地", "校区"];
					this.info = info.map(item => {
						let value = "";
						if (item == '性别') {
							if (userInfo.sex) {
								value = userInfo.sex == 1 ? '男': "女";
							}else {
								value = "暂未选择"
							}
						}else if (item == '手机号') {
							value = userInfo.phone;
						}else if (item == '职位') {
							value = userInfo.roleName;
						}else if (item == '营地') {
							value = userInfo.place;
						}else if (item == '校区') {
							value = userInfo.school;
						}
						return {
							label: item,
							value
						}
					})
				});
			},
			
			go(item) {
				if (item.name == '联系我们') return;
				if (this.isLogin == false) {
					this.openAuto();
				} else {
					uni.navigateTo({
						url: item.url
					})
				}
			},


			bindEdit(name) {
				if (app.globalData.isIframe) {
					window.parent.postMessage({
							name: name
						},
						'*'
					);
					return;
				}
			},
			menusTap(url) {
				if (this.isLogin == false) {
					this.openAuto();
				} else {
					goPage().then(res => {
						uni.navigateTo({
							url: url
						})
					})
				}

			},
			navito(e) {
				if (!app.globalData.isIframe) {
					window.location.href = 'https://' + e;
				} else {
					return false
				}
			},
			kefuClick() {
				if (this.chatConfig.telephone_service_switch == '1') {
					uni.makePhoneCall({
						phoneNumber: this.chatConfig.consumer_hotline //仅为示例
					});
				} else {
					return false
				}
			},
			getOrderData() {
				let that = this;
				orderData().then(res => {
					that.orderMenu.forEach((item, index) => {
						switch (item.title) {
							case '待付款':
								item.num = res.data.unPaidCount
								break
							case '待付尾款':
								item.num = res.data.unShippedCount
								break
							case '待开营':
								item.num = res.data.receivedCount
								break
							case '待评价':
								item.num = res.data.evaluatedCount
								break
							case '售后/退款':
								item.num = res.data.refundCount
								break
						}
					})
					that.$set(that, 'orderMenu', that.orderMenu);
				})
			},
			// 打开授权
			openAuto() {
				Cache.set(BACK_URL, '')
				toLogin();
			},
			// 授权回调
			onLoadFun() {
				this.getMyMenus();
				this.getOrderData();
			},
			// 绑定手机
			bindPhone() {
				uni.navigateTo({
					animationType: animationType.type,
					animationDuration: animationType.duration,
					url: '/pages/users/app_login/index'
				})
			},
			/**
			 * 
			 * 获取个人中心图标
			 */
			getMyMenus: function() {
				let that = this;
				getMenuList().then(res => {
					that.$set(that, 'MyMenus', res.data.routine_my_menus);
					that.wechatUrl = res.data.routine_my_menus.filter((item) => {
						return item.url.indexOf('service') !== -1
					})
					res.data.routine_my_menus.map((item) => {
						if (item.url.indexOf('service') !== -1) that.servicePic = item.pic
					})
					// that.imgUrls = res.data.routine_my_banner
					if (res.data.routine_my_banner) {
						that.imgUrls = res.data.routine_my_banner
					}
				});
			},
			// 编辑页面
			goEdit() {
				if (this.isLogin == false) {
					this.openAuto();
				} else {
					uni.navigateTo({
						animationType: animationType.type,
						animationDuration: animationType.duration,
						url: '/pages/summer/user_info/index'
					})
				}
			},
			// goMenuPage
			goMenuPage(url) {
				if (this.isLogin) {
					uni.navigateTo({
						animationType: animationType.type,
						animationDuration: animationType.duration,
						url
					})
				} else {
					this.openAuto();
				}
			},
			appUpdate() {
				uni.navigateTo({
					url: '/pages/users/app_update/app_update',
					animationType: animationType.type,
					animationDuration: animationType.duration,
				})
			},
			shareApi: function() {
				getShare().then(res => {
					this.$set(this, 'configApi', res.data);
				})
			},
			// 微信分享；
			setOpenShare: function(data) {
				let that = this;
				if (that.$wechat.isWeixin()) {
					let configAppMessage = {
						desc: data.synopsis,
						title: data.title,
						link: location.href,
						imgUrl: data.img
					};
					that.$wechat.wechatEvevt(["updateAppMessageShareData", "updateTimelineShareData"],
						configAppMessage);
				}
			},
			// 获取地址数据
			getCityList: function() {
				let that = this;
				getCity().then(res => {
					let oneDay = 24 * 3600 * 1000;
					this.$Cache.setItem({
						name: 'cityList',
						value: res.data,
						expires: oneDay * 7
					}); //设置七天过期时间
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page,
	body {
		height: 100%;
	}

	.mp-header {
		@include main_bg_color(theme);
	}

	.bg {
		// position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 350rpx;
		line-height: 200rpx;
		background-image: url('https://lives-1308318918.cos.ap-shanghai.myqcloud.com/images/user_bg.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;
		background: linear-gradient(90deg, #9FD998 0%, #66BBB1 100%);
		display: flex;
		justify-content: center;
		color: white;
		font-size: 40rpx;
		border-bottom-left-radius: 50rpx;
		border-bottom-right-radius: 50rpx;
	}

	.contenBox {
		padding: 0 30rpx;
	}

	.support {
		width: 219rpx;
		height: 74rpx;
		margin: 54rpx auto;
		display: block;
	}

	.new-users {
		display: flex;
		flex-direction: column;
		height: 100%;

		.sys-head {
			position: relative;
			width: 100%;
			background: linear-gradient(90deg, #9FD998 0%, #66BBB1 100%);

			.sys-title {
				z-index: 10;
				position: relative;
				height: 43px;
				text-align: center;
				line-height: 43px;
				font-size: 36rpx;
				color: #FFFFFF;
			}
		}

		.head {
			// @include index-gradient(theme);
			// background: linear-gradient(90deg, #9FD998 0%, #66BBB1 100%);
			position: absolute;
			top: 250rpx;
			width: 100%;

			.user-info,
			.order-wrapper,
			.study-info-btn,
			.function-list {
				background-color: white;
				padding: 30rpx;
			}

			.function-list {
				border-radius: 28rpx;

				.list-item {
					position: relative;
					padding: 20rpx 0;
					display: flex;
					justify-content: space-between;
					.item-left {
						display: flex;
						justify-content: space-between;
					}
					.img {
						width: 40rpx;
						height: 40rpx;
					}
					.name {
						margin-left: 30rpx;
					}

					.name,
					.arrow {
						font-size: 32rpx;
					}
				}
			}

			.study-info-btn {
				display: flex;
				justify-content: space-around;
				align-items: center;
				margin-bottom: 20rpx;
				border-bottom-left-radius: 28rpx;
				border-bottom-right-radius: 28rpx;

				button {
					border-radius: 48rpx;

					text {
						font-weight: 500;
						font-size: 30rpx;
					}
				}

				.student-info {
					vertical-align: middle;
					color: #EC5858;
					background-color: #FFB9B9;
					padding: 0 20rpx !important;
					display: flex;
					justify-content: center;
					align-items: center;

					image {
						width: 50rpx;
						height: 50rpx;
						vertical-align: middle;
						margin-right: 5rpx;
					}

					text {
						margin-right: 5rpx;
						vertical-align: middle;
					}
				}

				.class-info {
					vertical-align: middle;
					background-color: #9FAAFF;
					color: #3C4485;
					padding: 0 20rpx !important;
					display: flex;
					justify-content: center;
					align-items: center;

					image {
						width: 50rpx;
						height: 50rpx;
						vertical-align: middle;
						margin-right: 5rpx;
					}

					text {
						margin-right: 5rpx;
						vertical-align: middle;
					}
				}
			}

			.user-info {
				border-top-left-radius: 28rpx;
				border-top-right-radius: 28rpx;
				z-index: 20;
				position: relative;
				display: flex;

				.avatar {
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
				}

				.info {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					margin-left: 20rpx;
					padding: 10rpx 0;
					position: relative;

					.name {
						display: flex;
						align-items: center;
						color: #161D1A;
						font-size: 32rpx;
						font-weight: 600;

						.vip {
							display: flex;
							align-items: center;
							padding: 6rpx 20rpx;
							background: rgba(0, 0, 0, 0.2);
							border-radius: 18px;
							font-size: 20rpx;
							margin-left: 12rpx;

							image {
								width: 27rpx;
								height: 27rpx;
							}
						}
					}

					.app_set {
						position: absolute;
						font-size: 36rpx;
						color: #fff;
						top: 40rpx;
						right: 20rpx;
					}

					.num {
						display: flex;
						align-items: center;
						font-size: 26rpx;
						color: rgba(255, 255, 255, 0.6);

						.num-txt {
							color: #C0C4C1;
							font-size: 30rpx;
							font-weight: 400;
							display: flex;
							align-items: center;
							margin-top: 20rpx;

							image {
								width: 40rpx;
								height: 40rpx;
							}
						}

						image {
							width: 22rpx;
							height: 23rpx;
							// margin-left: 20rpx;
						}
					}
				}

				.user-card {
					position: relative;
					width: 100%;
					margin: 0 auto;
					padding: 35rpx 0 30rpx 0;

				}

				.num-wrapper {
					z-index: 30;
					position: relative;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 30rpx;
					color: #fff;

					.num-item {
						width: 33.33%;
						text-align: center;

						.num {
							font-size: 42rpx;
							font-weight: bold;
						}

						.txt {
							margin-top: 10rpx;
							font-size: 26rpx;
							color: rgba(255, 255, 255, 0.6);
						}
					}
				}

				.sign {
					z-index: 200;
					position: absolute;
					right: -12rpx;
					top: 80rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 120rpx;
					height: 60rpx;
					background: linear-gradient(90deg, rgba(255, 225, 87, 1) 0%, rgba(238, 193, 15, 1) 100%);
					border-radius: 29rpx 4rpx 4rpx 29rpx;
					color: #282828;
					font-size: 28rpx;
					font-weight: bold;
				}
			}

			.order-wrapper {
				background-color: #fff;
				border-radius: 28rpx;
				position: relative;
				margin-bottom: 20rpx;
				z-index: 11;

				.order-hd {
					justify-content: space-between;
					font-size: 30rpx;
					color: #282828;
					margin-bottom: 40rpx;
					padding: 0 16rpx;

					.left {
						color: #282828;
						font-size: 30rpx;
						font-weight: 600;
					}

					.right {
						align-items: center;
						color: #666666;
						font-size: 26rpx;

						.icon-xiangyou {
							margin-left: 5rpx;
							font-size: 24rpx;
						}
					}
				}

				.order-bd {
					display: flex;
					justify-content: space-between;
					padding: 0;

					.order-item {
						width: 25%;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;

						.pic {
							position: relative;
							text-align: center;

							image {
								width: 48rpx;
								height: 48rpx;
							}
						}

						.txt {
							margin-top: 15rpx;
							font-size: 26rpx;
							color: #454545;
						}
					}
				}
			}
		}

		.slider-wrapper {
			margin: 20rpx 0;
			height: 138rpx;

			swiper,
			swiper-item {
				height: 100%;
			}

			image {
				width: 100%;
				height: 100%;
			}
		}

		.user-menus {
			background-color: #fff;
			border-radius: 14rpx;

			.menu-title {
				padding: 30rpx 30rpx 40rpx;
				font-size: 30rpx;
				color: #282828;
				font-weight: 600;
			}

			.list-box {
				display: flex;
				flex-wrap: wrap;
				padding: 0;
			}

			.item {
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;
				flex-direction: column;
				width: 25%;
				margin-bottom: 47rpx;
				font-size: 26rpx;
				color: #333333;

				image {
					width: 52rpx;
					height: 52rpx;
					margin-bottom: 18rpx;
				}


				&:last-child::before {
					display: none;
				}
			}

			button {
				font-size: 28rpx;
			}
		}

		.phone {
			color: #fff;
		}

		.pic_status {
			width: 48rpx;
			height: 48rpx;
		}

		.order-status-num {
			border-radius: 50%;
			position: absolute;
			right: -15rpx;
			top: -15rpx;
			font-size: 20rpx;
			padding: 0 8rpx;
			color: white;
			background-color: #F2450F;
		}

	}

	.sub_btn {
		width: 690rpx;
		height: 86rpx;
		line-height: 86rpx;
		margin-top: 60rpx;
		background: $theme-color;
		border-radius: 43rpx;
		color: #fff;
		font-size: 28rpx;
		text-align: center;
	}

	.arrow_img {
		width: 24rpx;
		height: 24rpx;
	}
	
	.border_line {
		border-bottom: solid 2rpx #eee;
	}
</style>